<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\Stat\StatDailyCall;
use App\Model\Stat\StatOnlineLog;
use App\Model\Stat\StatPayLog;
use App\Model\Stat\StatRegLog;
use App\Model\UserProfile;
use App\Model\VideoCall;
use App\Model\VoiceCall;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Echarts\Echarts;
use Encore\Admin\Widgets\Form as WidgetsForm;

class HomeController extends Controller
{
    use ModelForm;

    public function index()
    {
        $showOnline = env('HOME_SHOW_ONLINE', true);
        if (!$showOnline || !Admin::user()->inRoles(['administrator', 'admin_normal', 'temp','cjtgy'])) {
            $content = new Content();
            $content->header('后台运营系统');
            return $content;
        }
        $content = new Content();

        //在线人数
        $content->row(function (Row $row) {
            $echarts = (new Echarts())
                ->setData($this->getOnlineInfo())
                ->bindLegend([
                    'count_time' => '时间',
                    'online_user_num' => '在线普通用户数',
                    'online_host_num' => '在线女用户数',
                    'online_num' => '在线总用户数'
                ]);
            $where = ['gender' => 1, 'online' => 1];


            $onlineMale = UserProfile::where($where)->count();
            $adminId = Admin::user()->id;
            if($adminId == 50){
                $base = 4;
                $onlineMale = $onlineMale*$base;
            }
            $where['gender'] = 0;
            $onlineFeMale = UserProfile::where($where)->count();
            $box = new Box("实时在线人数（当前男：{$onlineMale}；女：{$onlineFeMale}）");
            $box->content($echarts);
            $row->column(10, $box);
        });

        //注册人数
        $content->row(function (Row $row) {
            $echarts = (new Echarts())
                ->setData($this->getRegInfo())
                ->bindLegend([
                    'count_time' => '时间',
                    'reg_num' => '总用户数',
                    'reg_female_num' => '女用户数',
                    'reg_male_num' => '男用户数',
                ]);
            $box = new Box('注册人数');
            $box->content($echarts);
            $row->column(10, $box);
        });

        $adminId = Admin::user()->id;
        if($adminId != 50){
            //充值
            $content->row(function (Row $row) {
                $echarts = (new Echarts())
                    ->setData($this->getPayInfo())
                    ->bindLegend([
                        'count_time' => '时间',
                        'pay_rmb_num' => '充值总额',
                        'wechat_rmb_num' => '微信充值',
                        'alipay_rmb_num' => '支付宝充值'
                    ]);
                $box = new Box('充值统计');
                $box->content($echarts);
                $row->column(10, $box);
            });
        }


        //话单趋势
        $content->row(function (Row $row) {
            $echarts = (new Echarts())
                ->setData($this->getCallInfo())
                ->bindLegend([
                    'count_time' => '时间',
                    'voice_total_num' => '语音总话单',
                    'voice_num' => '语音成功话单',
                    'video_total_num' => '视频总话单',
                    'video_num' => '视频成功话单',
                ]);
            $box = new Box('话单统计');
            $box->content($echarts);
            $row->column(10, $box);
        });
        return $content;
    }

    function getOnlineInfo()
    {
        $data = StatOnlineLog::getData(date('Y-m-d', strtotime('-1 day')));
        $ret = [];
        $adminId = Admin::user()->id;
        foreach ($data as $k => $item) {
            if ($k % 3 > 0) continue;
            if($adminId == 50){
                $ret[] = [
                    'count_time' => date('m-d H:i', $item['stat_ts']),
                    'online_user_num' => $item['online_user_num'] * 4,
                    'online_host_num' => $item['online_host_num'],
                    'online_num' => $item['online_user_num'] * 4 + $item['online_host_num'],
                ];
            }else{
                $ret[] = [
                    'count_time' => date('m-d H:i', $item['stat_ts']),
                    'online_user_num' => $item['online_user_num'],
                    'online_host_num' => $item['online_host_num'],
                    'online_num' => $item['online_num'],
                ];
            }

        }
        return $ret;
    }

    function getRegInfo()
    {
        $data = StatRegLog::getData(date('Y-m-d', strtotime('-1 day')));
        $ret = [];
        $adminId = Admin::user()->id;
        foreach ($data as $k => $item) {
            if ($k % 3 > 0) continue;
            if($adminId == 50){
                $ret[] = [
                    'count_time' => date('m-d H:i', $item['stat_ts']),
                    'reg_female_num' => $item['reg_female_num'] * 4,
                    'reg_male_num' => $item['reg_male_num'],
                    'reg_num' => $item['reg_female_num'] * 4 + $item['reg_male_num'],
                ];
            }else{
                $ret[] = [
                    'count_time' => date('m-d H:i', $item['stat_ts']),
                    'reg_female_num' => $item['reg_female_num'],
                    'reg_male_num' => $item['reg_male_num'],
                    'reg_num' => $item['reg_num'],
                ];
            }

        }
        return $ret;
    }

    /**
     * 话单趋势信息
     */
    function getCallInfo()
    {
        $data = StatDailyCall::getData(date('Y-m-d', strtotime('-20 day')));
        $ret = [];
        foreach ($data as $k => $item) {
            // if ($k % 3 > 0) continue;
            if (!isset($ret[$item['dt']]))
                $ret[$item['dt']] = [
                    'count_time' => $item['dt'],
                    'voice_total_num' => 0,
                    'voice_num' => 0,
                    'video_total_num' => 0,
                    'video_num' => 0,
                ];
            if ($item['call_type'] == VoiceCall::CALL_TYPE) {
                $ret[$item['dt']]['voice_total_num'] = $item['call_num'];
                $ret[$item['dt']]['voice_num'] = $item['success_num'];
            }
            if ($item['call_type'] == VideoCall::CALL_TYPE) {
                $ret[$item['dt']]['video_total_num'] = $item['call_num'];
                $ret[$item['dt']]['video_num'] = $item['success_num'];
            }
        }
        return array_values($ret);
    }

    function getPayInfo()
    {
        $data = StatPayLog::getData(date('Y-m-d', strtotime('-1 day')));
        $ret = [];
        foreach ($data as $item) {
            $ret[] = [
                'count_time' => date('m-d H:i', $item['stat_ts']),
                'pay_rmb_num' => $item['pay_rmb_num'] / 100,
                'wechat_rmb_num' => $item['wechat_rmb_num'] / 100,
                'alipay_rmb_num' => $item['alipay_rmb_num'] / 100,
            ];
        }
        return $ret;
    }
}
